package org.eclipse.epsilon.emc.spreadsheets.csv;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.epsilon.emc.spreadsheets.SpreadsheetColumn;
import org.eclipse.epsilon.emc.spreadsheets.SpreadsheetRow;

public class CsvRow extends SpreadsheetRow
{
	private LinkedList<String> row;

	public CsvRow(final CsvWorksheet worksheet, final String row)
	{
		super(worksheet);
		this.row = new LinkedList<String>();
		this.parseRow(row);
	}

	private void parseRow(final String row)
	{
		final CsvModel model = (CsvModel) this.worksheet.getModel();
		final String[] values = row.split(model.getSeparator());
		if (values.length == 0)
		{
			for (int i = 0; i < this.worksheet.getHeader().getColumns().size(); i++)
			{
				this.row.add(this.worksheet.getDefaultEmptyCellValue());
			}
		}
		else
		{
			for (final String value : values)
			{
				this.row.add(value);
			}
		}
	}

	public List<String> getRow()
	{
		return new ArrayList<String>(this.row);
	}

	public void addValue(final String value)
	{
		this.row.add(value);
	}

	@Override
	public String getVisibleCellValue(final SpreadsheetColumn column)
	{
		if (this.row.size() > column.getIndex())
		{
			return this.row.get(column.getIndex());
		}
		else
		{
			return null;
		}
	}

	@Override
	public void overwriteCellValue(final SpreadsheetColumn column, final String value)
	{
		this.row.set(column.getIndex(), value);
	}

	public void clear()
	{
		this.row.clear();
	}

}
